/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package argentracker.services.impl;

import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import argentracker.services.ChppApplication;
import argentracker.services.ChppException;

import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;

/**
 * briefly description.
 * 
 * extense description.
 * 
 * @author matero (matero _at_ gmail _dot_ com)
 */
public class ChppRequestBuilder {

    private String server;
    private String fileSpec;
    private final String file;
    private List<String> args = new LinkedList<String>();

    public ChppRequestBuilder(String chppFile) {
        this.file = chppFile;
    }

    public static ChppRequestBuilder get(String file) {
        return new ChppRequestBuilder(file);
    }

    public ChppRequestBuilder withChppFileSpec(String newFileSpec) {
        this.fileSpec = newFileSpec;
        return this;
    }

    public ChppRequestBuilder from(String hattrickServer) {
        this.server = hattrickServer;
        return this;
    }

    public ChppRequestBuilder ofVersion(double fileVersion) {
        this.args.add("version=" + fileVersion);
        return this;
    }

    public ChppRequestBuilder loginName(String loginName) {
        this.args.add("loginname=" + loginName);
        return this;
    }
    
    public ChppRequestBuilder securityCode(String readonlyPassword) {
        this.args.add("readonlyPassword=" + readonlyPassword);
        return this;
    }

    public ChppRequestBuilder actionType(String actionTypeName) {
        this.args.add("actionType=" + actionTypeName);
        return this;
    }

    public ChppRequestBuilder chppApp(ChppApplication application) {
        this.args.add("chppID=" + application.chppID);
        this.args.add("chppKey=" + application.chppKey);
        return this;
    }

    public HTTPRequest create() {
        return new HTTPRequest(url(), HTTPMethod.GET, disallowTruncate().doNotFollowRedirects());
    }

    protected URL url() {
        try {
            return new URL(buildUrl());
        } catch (MalformedURLException e) {
            throw new ChppException("illegal fetch file url", e);
        }
    }

    protected String buildUrl() {
        final StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(this.server).append(this.fileSpec).append(this.file);
        for (String requestArgument : this.args) {
            urlBuilder.append('&').append(requestArgument);
        }
        return urlBuilder.toString();
    }
}
